App({

  onLaunch: function(options) {
    var that = this;
    // 获取用户定位
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度
      success: res => {
        that.globalData.longitude = res.longitude;
        that.globalData.latitude = res.latitude;
      }
    });

    // 用户登录
    this.login();

    wx.getSystemInfo({
      success: function(res) {
        that.globalData.sysInfo = res;
        if (res.model.search('iPhone X') != -1) {
          that.globalData.isIphoneX = true;
        }
      },
    })
  },
  onShow: function(options) {

  },
  onHide: function(options) {

  },
  onError: function(options) {

  },
  // 全局变量
  globalData: {
    userInfo: null,
    imgUrl: '',
    requestUrl: 'http://bigita.com:8080/wx/api/',
    longitude: 0,
    latitude: 0,
    qqmapsdk: 'VDJBZ-GCLKF-HNVJZ-JBG6U-VQW65-K6FLA',
    sysInfo: {},
    isIphoneX: false
  },
  // 简写request请求
  request: function(url, data, method, fn) {
    var fullUrl = this.globalData.requestUrl + url;
    wx.request({
      url: fullUrl,
      data: data,
      header: {
        'content-type': 'application/json'
      },
      method: method,
      dataType: 'json',
      responseType: 'text',
      success: function(res) {
        //console.log(res);
        if (res.data.retCode == 0) {
          fn(res.data.retData);
        } else {
          wx.showModal({
            title: '错误提示',
            content: res.data.retMessage,
            showCancel: false
          })
        }

      },
      fail: function(res) {
        console.error(res);
      },
      complete: function(res) {
        setTimeout(wx.hideLoading, 500);
        wx.stopPullDownRefresh();
      },
    });
  },
  //json数组获取index
  jsonArrayIndex: function(jsonArray, key, value) {
    var length = jsonArray.length;
    var index = -1;
    for (var i = 0; i < length; i++) {
      if (jsonArray[i][key] == value) {
        index = i;
      }
    }
    return index;
  },

  // 登录
  login: function() {
    var that = this;
    wx.login({
      success: res => {
        if (res.code) {
          // var url = 'login';
          // that.request(url, { code: res.code }, 'GET', res => {
          //   // 获取用户信息
          //   wx.getUserInfo({
          //     lang: 'zh_CN',
          //     success: res => {
          //       that.globalData.userInfo = res.userInfo;
          //     }
          //   })
          // })
        }
      }
    })
  },

})